package com.treasure.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.treasure.common.model.PageResult;
import com.treasure.infrastructure.model.po.CorpInfo;
import com.treasure.infrastructure.model.po.CorpUserInfo;
import com.treasure.infrastructure.repository.dao.mapper.CorpInfoMapper;
import com.treasure.infrastructure.repository.dao.mapper.CorpUserInfoMapper;
import com.treasure.web.command.PageConvertor;
import com.treasure.web.command.request.CorpInfoQuery;
import com.treasure.web.command.response.CorpInfoResponse;
import com.treasure.web.mapstruct.CorpInfoAssembler;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author sunhongmin
 * @date 2023/11/5 19:50
 * @description
 */
@Service
@RequiredArgsConstructor
public class CorpManageService {
    private final CorpInfoMapper corpInfoMapper;
    private final CorpUserInfoMapper corpUserInfoMapper;

    // 企业列表
    public PageResult<CorpInfoResponse> listCorpInfo(CorpInfoQuery corpInfoQuery) {
        List<CorpInfo> corpInfoList = queryCorpInfoListPage(corpInfoQuery);
        PageInfo<CorpInfo> pageInfo = new PageInfo<>(corpInfoList);
        List<CorpInfoResponse> corpInfoResponses = CorpInfoAssembler.INSTANCE.toCorpInfoResponse(corpInfoList);
        // 企业人数
        Map<String, Long> corpUserGroupCount = getCorpUserGroupCount();
        for (CorpInfoResponse corpInfoResponse : corpInfoResponses) {
            corpInfoResponse.setUserCount(Optional.ofNullable(corpUserGroupCount.get(corpInfoResponse.getCorpId())).map(Long::intValue).orElse(0));
        }
        return PageConvertor.convertPage(pageInfo, corpInfoResponses);
    }

    @NotNull
    private Map<String, Long> getCorpUserGroupCount() {
        LambdaQueryWrapper<CorpUserInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(CorpUserInfo::getCorpId,CorpUserInfo::getId);
        List<CorpUserInfo> users = corpUserInfoMapper.selectList(lambdaQueryWrapper);
        return users.stream().collect(Collectors.groupingBy(CorpUserInfo::getCorpId, Collectors.counting()));
    }

    private List<CorpInfo> queryCorpInfoListPage(CorpInfoQuery corpInfoQuery) {
        QueryWrapper<CorpInfo> corpQuery = new QueryWrapper<>();
        if (StringUtils.isNotBlank(corpInfoQuery.getCorpNameLike())) {
            corpQuery.lambda().eq(CorpInfo::getCorpName, corpInfoQuery.getCorpNameLike());
        }
        if (StringUtils.isNotBlank(corpInfoQuery.getInviteCode())) {
            corpQuery.lambda().eq(CorpInfo::getInviteCode, corpInfoQuery.getInviteCode());
        }
        if (corpInfoQuery.getHasDeleted() != null) {
            corpQuery.lambda().eq(CorpInfo::getHasDeleted, corpInfoQuery.getHasDeleted());
        }
        if (corpInfoQuery.getHasOpen() != null) {
            corpQuery.lambda().eq(CorpInfo::getHasOpen, corpInfoQuery.getHasOpen());
        }

        PageHelper.startPage(corpInfoQuery.getPage(), corpInfoQuery.getPageSize());
        return corpInfoMapper.selectList(corpQuery);
    }

    // 新建企业

    // 统计报表

    // 新建企业管理员
}
